/*
 * KB_ASSET -- Version 1.0 (C) Copyright 2004-2009 Koubei.com Inc
 * by biluochun@alibaba-inc.com
 *
*/
#include <stdio.h>
#include <stdlib.h>
#include <alloc.h>

#define MAX_ITEM_SIZE 64
#define DICT_ITEM_SIZE 10000

struct dict_s {
	char sn[MAX_ITEM_SIZE];
	char asset[MAX_ITEM_SIZE];
};

static const char *version_str = "KB_ASSET -- Version 1.1 (C) Copyright 2004-2009 Koubei.com Inc";
static const char *dictfile = "data.csv";
static const char *resfile = "res";
static const char *run_asset_bat_file = "KB_ASSET.BAT";
static const char *resmagic = "ASSET -- Version 2.09 ";
static const char *datamagic = "sn,asset";
static const char *tagstart = "Service Tag => ";
static const char *tagend = "\n";
static const char *asset_token = ",";
static const char *run_asset_bat_template = 
					"asset %s < y\n"
					"asset /o %s < y\n"
					"cd c:\\\n";

static void version()
{
	printf("%s\n", version_str);
}

static errormsg()
{
	printf("\n*******please contact mifeng@alibaba-inc.com*******\n");
}

static void usage()
{
}

/*
*  return 1 if success, other is fail
*/
static int serviceTag(char *sertag, int sertagsize)
{
	char *s, *e;
	FILE *fd;
	char buf[1024];
	size_t l;
	fd = fopen(resfile, "r");
	if (fd == NULL)
	{
		printf("open %s fail !!!", resfile);
		errormsg();
		return 0;
	}
	
	fread(buf, 1024, sizeof(char), fd);
	if (strncmp(buf, resmagic, strlen(resmagic)))
	{
		buf[strlen(resmagic)] = '\0';
		printf("not magic([%s]<--->[%s]) start !!!", resmagic, buf);
		fclose(fd);
		errormsg();
		return 0;
	}

	s = strstr(buf, tagstart);
	if (s == NULL)
	{
		printf("no find tag start [%s] !!!\n", tagstart);
		fclose(fd);
		errormsg();
		return 0;
	}
	e = strstr(s, tagend);
	if (e == NULL)
	{
		printf("no find tag end [%s] !!!\n", tagend);
		fclose(fd);
		errormsg();
		return 0;
	}
	l = e-s-strlen(tagstart);
	if (l >= sertagsize)
	{
		printf("tag length(%d) >= buffer max size(%d)\n", l, sertagsize);
		fclose(fd);
		errormsg();
		return 0;
	}
	memcpy(sertag, s+strlen(tagstart), l);
	sertag[l] = '\0';
	fclose(fd);
	return 1;
}

static char *____fgets(char *s, int n, FILE *stream)
{
	char c;
	int t = n;
	while ((c = (char)fgetc(stream)) != EOF && c != '\n' && n-1 > 0) {
		*s = c;
		s++;
		n--;
	}
	if (c == EOF && t == n)
		return NULL;
	*s = '\0';
	return s;
}

static struct dict_s *make_dict(const char* dictname)
{
	FILE *fd;
	char buf[256];
	char *sn;
	char *asset;
	struct dict_s *dict;
	int idx = 0;
	fd = fopen(dictname, "r");
	if (fd == NULL)
	{
		printf("open %s fail !!!\n", dictname);
		errormsg();
		return NULL;
	}
	dict = (struct dict_s*)malloc(sizeof(struct dict_s) * DICT_ITEM_SIZE);
	if (dict == NULL)
	{
		printf("memory out!!");
		fclose(fd);
		errormsg();
		return NULL;
	}
	memset(dict, 0, sizeof(struct dict_s) * DICT_ITEM_SIZE);
	if (!____fgets(buf, 256, fd) || strncmp(buf, datamagic, strlen(datamagic))) {
		printf("not magic([%s]<--->[%s]) start !!!", datamagic, buf);
		fclose(fd);
		errormsg();
		return NULL;
	}
	while (____fgets(buf, 256, fd)) {
		if (idx >= DICT_ITEM_SIZE-1) {
			printf("item out!!\n");
			fclose(fd);
			errormsg();
			return NULL;
		}
		sn = buf;
		asset = strstr(sn, asset_token);
		*asset = '\0';
		asset++;
		strncpy(dict[idx].sn, sn, MAX_ITEM_SIZE);
		strncpy(dict[idx].asset, asset, MAX_ITEM_SIZE);		
		idx++;
	}
	fclose(fd);
	return dict;
}

static void clean_dict(struct dict_s *dict)
{
	free(dict);
}

static char *find_asset(struct dict_s *dict, const char *sn)
{
	int idx = 0;
	int len = strlen(sn);
	while (*dict[idx].sn != '\0' && *dict[idx].asset != '\0') {
		if (!strncmp(dict[idx].sn, sn, len))
			return dict[idx].asset;
		idx++;
	}
	return NULL;
}

static int out_run_asset_bat(const char *asset)
{
	char buf[1024];
	FILE *fd;

	sprintf(buf, run_asset_bat_template, asset, asset);
	fd = fopen(run_asset_bat_file, "w");
	if (fd == NULL) {
		printf("out run asset[%s] bat failed\n", run_asset_bat_file);
		errormsg();
		return 0;
	}
	fwrite(buf, sizeof(char), strlen(buf), fd);
	fclose(fd);
	printf("out run asset[%s] bat success\n", run_asset_bat_file);
	return 1;
}

int main()
{
	char sertag[256];
	char *asset;
	struct dict_s *dict;
	version();
	if (!serviceTag(sertag, 256))
		exit(0);
	printf("service Tag => %s\n", sertag);
	dict = make_dict(dictfile);
	if (dict == NULL)
		exit(0);
	printf("load dict success!\n");
	asset = find_asset(dict, sertag);
	if (asset != NULL) {
		printf("find asset[%s] success\n", asset);
		out_run_asset_bat(asset);
	} else {
		printf("find asset failed !!!");
		errormsg();
	}
	clean_dict(dict);
	return 0;
}
